package com.byedu.domain.course.vo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.byedu.domain.course.*;
import com.byedu.util.CommonUtil;
import com.byedu.util.yunpian.util.StringUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户与课程信息关联实体
 * Created by jason on 2017/5/21.
 */
public class UserLibraryVO implements Serializable {



    /*试卷  对应bay_library的主键*/
    private Integer libraryId;

    private String libraryName;
    /*答题花费时间*/
    private int consumedTime;

//    private String serialno ;
    /*错误题数*/
    private int wrongSum;

    /*正确题数*/
    private int rightSum;

//    /*总题数*/
//    private int total;

    /*得分*/
    private int score;

    /*排名*/
    private int rankNo=1;

    /*排名*/
    private int process;
    /*答案列表*/
    private List<AnswerCheck> answerList ;


    public Integer getLibraryId() {
        return libraryId;
    }

    public void setLibraryId(Integer libraryId) {
        this.libraryId = libraryId;
    }

    public int getConsumedTime() {
        return consumedTime;
    }

    public void setConsumedTime(int consumedTime) {
        this.consumedTime = consumedTime;
    }

    public int getWrongSum() {
        return wrongSum;
    }

    public void setWrongSum(int wrongSum) {
        this.wrongSum = wrongSum;
    }

    public int getRightSum() {
        return rightSum;
    }

    public void setRightSum(int rightSum) {
        this.rightSum = rightSum;
    }

//    public int getTotal() {
//        return total;
//    }
//
//    public void setTotal(int total) {
//        this.total = total;
//    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }


    public UserLibraryVO() {

    }

    public UserLibraryVO(UserLibraryDO userLibraryDO ) {
        /*主键*/
        this.answerList = new ArrayList<AnswerCheck>();
        this.libraryId = userLibraryDO.getLibraryId();
        this.consumedTime = userLibraryDO.getConsumedTime();
        List<UserQuestionErrDO> userQuestionErrDOList = new ArrayList<UserQuestionErrDO>();
        if (!StringUtil.isNullOrEmpty(userLibraryDO.getAnswerlist())){
//            System.out.println("----" + userLibraryDO.getAnswerlist());
            JSONArray answerJsonLists = JSONArray.parseArray(userLibraryDO.getAnswerlist());
            this.wrongSum = 0;
            this.rightSum = 0;
            for (int i = 0; i < answerJsonLists.size(); i++) {
                JSONObject jsonObjectb = (JSONObject) answerJsonLists.get(i);
                UserAnswer itemPostAnswer = JSON.parseObject(jsonObjectb.toJSONString(), UserAnswer.class);
                System.out.print(itemPostAnswer.toString());
                AnswerCheck answerCheck = new AnswerCheck();
//                int ti = itemPostAnswer.getId();
                answerCheck.setId(itemPostAnswer.getId());
                answerCheck.setA(itemPostAnswer.getA());
                answerCheck.setR(itemPostAnswer.getR());
                answerCheck.setNo(itemPostAnswer.getNo());
                //将传上来的答案进行排序。
                if (CommonUtil.stringsort(answerCheck.getA()).equals(CommonUtil.stringsort(answerCheck.getR()))) {
                    answerCheck.setC(true);
                    this.rightSum++;
                } else {

                    UserQuestionErrDO userQuestionErrDO = new UserQuestionErrDO();
                    userQuestionErrDO.setUserId(userLibraryDO.getUserId());
                    userQuestionErrDO.setCreateTime(userLibraryDO.getCreatedTime());
                    userQuestionErrDO.setSubOption(answerCheck.getA());
                    userQuestionErrDO.setRightOption(answerCheck.getR());
                    userQuestionErrDO.setQuestionId(itemPostAnswer.getId());
                    userQuestionErrDOList.add(userQuestionErrDO);
                    answerCheck.setC(false);
                    this.wrongSum++;
                }
                this.answerList.add(answerCheck);
             }

        }
        userLibraryDO.setRightSum(this.rightSum);
        userLibraryDO.setWrongSum(this.wrongSum);
        userLibraryDO.setScore(this.rightSum);
        userLibraryDO.setUserQuestionErrDOList(userQuestionErrDOList);
        this.score = this.rightSum ;
    }


    public UserLibraryVO(UserLibraryDO userLibraryDO ,LibraryDO libraryDO) {
        /*主键*/

            this.answerList = new ArrayList<AnswerCheck>();
            this.libraryId = userLibraryDO.getLibraryId();
            this.libraryName = libraryDO.getName();
            this.consumedTime = userLibraryDO.getConsumedTime();
            List<UserQuestionErrDO> userQuestionErrDOList = new ArrayList<UserQuestionErrDO>();
            if (!StringUtil.isNullOrEmpty(userLibraryDO.getAnswerlist())) {
//            System.out.println("----" + userLibraryDO.getAnswerlist());
                JSONArray answerJsonLists = JSONArray.parseArray(userLibraryDO.getAnswerlist());
                this.wrongSum = 0;
                this.rightSum = 0;
                for (int i = 0; i < answerJsonLists.size(); i++) {
                    JSONObject jsonObjectb = (JSONObject) answerJsonLists.get(i);
                    UserAnswer itemPostAnswer = JSON.parseObject(jsonObjectb.toJSONString(), UserAnswer.class);
//                System.out.print(itemPostAnswer.toString());
                    AnswerCheck answerCheck = new AnswerCheck();
//                int ti = itemPostAnswer.getId();
                    answerCheck.setId(itemPostAnswer.getId());
                    answerCheck.setA(itemPostAnswer.getA());
                    answerCheck.setR(itemPostAnswer.getR());
                    answerCheck.setNo(itemPostAnswer.getNo());
                    //将传上来的答案进行排序。
                    if (CommonUtil.stringsort(answerCheck.getA()).equals(CommonUtil.stringsort(answerCheck.getR()))) {
                        answerCheck.setC(true);
                        this.rightSum++;
                    } else {

                        UserQuestionErrDO userQuestionErrDO = new UserQuestionErrDO();
                        userQuestionErrDO.setUserId(userLibraryDO.getUserId());
                        userQuestionErrDO.setCreateTime(userLibraryDO.getCreatedTime());
                        userQuestionErrDO.setSubOption(answerCheck.getA());
                        userQuestionErrDO.setRightOption(answerCheck.getR());
                        userQuestionErrDO.setQuestionId(itemPostAnswer.getId());
                        userQuestionErrDOList.add(userQuestionErrDO);
                        answerCheck.setC(false);
                        this.wrongSum++;
                    }
                    this.answerList.add(answerCheck);
                }

            }
            userLibraryDO.setRightSum(this.rightSum);
            userLibraryDO.setWrongSum(this.wrongSum);
            userLibraryDO.setScore(this.rightSum);
            userLibraryDO.setUserQuestionErrDOList(userQuestionErrDOList);
            this.score = this.rightSum;
    }
    public int getRankNo() {
        return rankNo;
    }

    public void setRankNo(int rankNo) {
        this.rankNo = rankNo;
    }


    public List<AnswerCheck> getAnswerList() {
        return answerList;
    }

    public void setAnswerList(List<AnswerCheck> answerList) {
        this.answerList = answerList;
    }

    @Override
    public String toString() {
        return "UserLibraryVO{" +
                "libraryId=" + libraryId +
                ", consumedTime=" + consumedTime +
                ", wrongSum=" + wrongSum +
                ", rightSum=" + rightSum +
                ", score=" + score +
                ", rankNo=" + rankNo +
                ", answerList=" + answerList +
                '}';
    }

    public int getProcess() {
        return process;
    }

    public void setProcess(int process) {
        this.process = process;
    }

    public String getLibraryName() {
        return libraryName;
    }

    public void setLibraryName(String libraryName) {
        this.libraryName = libraryName;
    }
}
